package com.klun.aop.factory;

import com.klun.aop.CGlib.CGLibProxy;
import com.klun.aop.CGlib.Person;
import com.klun.aop.JDKProxy.DynamicProxy;
import com.klun.aop.JDKProxy.PersonImpl;
import com.klun.aop.speak.Speakable;

/**
 * 代理工厂
 */
public class PersonProxyFactory {

    /**
     * JDK动态代理
     * @return
     */
    public static Speakable newJdkProxy() {
        // 代理PersonImpl
        DynamicProxy dynamicProxy = new DynamicProxy(new PersonImpl());
        Speakable proxy = dynamicProxy.getProxy();
        return proxy;
    }

    /**
     * CGlib动态代理
     * @return
     */
    public static Person newCglibProxy() {
        CGLibProxy cglibProxy = CGLibProxy.getInstance();
        Person proxy = cglibProxy.getProxy(Person.class);
        return proxy;
    }

    public static void main(String[] args) {
        // jdk dynamic proxy
//        System.out.println("******** jdk dynamic proxy ******** ");
//        Speakable jdkProxy1 = PersonProxyFactory.newJdkProxy();
//        jdkProxy1.sayHi();
//        System.out.println();
//        System.out.println();
//        jdkProxy1.sayBye();

        System.out.println("******** cglib dynamic proxy ******** ");
        Person person = PersonProxyFactory.newCglibProxy();
        System.out.println(person.getClass().getName()+":****当前线程::"+Thread.currentThread().getName());
        person.sayHi();
        System.out.println();
        System.out.println();
        person.sayBye();


    }

}
